package models;

import helpers.GeneralHelper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.cache.Cache;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

import entities.AutoSite;

@SuppressWarnings("serial")
public class AutoSiteModel implements Serializable {
private Objectify ofy;
	
	private Cache cache = null;
	
	private String prefix = "email_";
	
	private static AutoSiteModel instance = null;
	
	public static AutoSiteModel getInstance() {
		if (instance == null) {
			instance = new AutoSiteModel();
		}
		
		return instance;
	}
	
	private AutoSiteModel() {
		try {
			ObjectifyService.register(AutoSite.class);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		cache = GeneralHelper.getCache();
		ofy = GeneralHelper.getObjectify();
	}
	
	public AutoSite get(String id) {
		AutoSite autoSite = new AutoSite();
		
		try {
			autoSite = ofy.get(new Key<AutoSite>(AutoSite.class, id));
		} catch (Exception e) {
			autoSite = null;
		}
		
		return autoSite;
	}
	
	public List<AutoSite> list(int page, int limit) {
		List<AutoSite> sites = new ArrayList<AutoSite>();
		
		int start = (page - 1) * limit;
		sites = ofy.query(AutoSite.class).limit(limit).offset(start).list();
		
		return sites;
	}
	
	public void insert(AutoSite autoSite) {
		ofy.put(autoSite);
	}
	
	public void delete(AutoSite autoSite) {
		ofy.delete(autoSite);
	}
}
